package org.linlinjava.litemall.db.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.linlinjava.litemall.db.domain.LitemallOrderTotalReturn;
import org.linlinjava.litemall.db.domain.LitemallOrderTotalReturnHistory;
import org.linlinjava.litemall.db.domain.ReturnGoodsList;
import org.linlinjava.litemall.db.dto.RentPartQueryParam;

import java.util.List;


public interface LitemallOrderTotalReturnHistoryMapper extends
        BaseMapper<LitemallOrderTotalReturnHistory> {


    @Select("SELECT " +
            "    lrod.goods_id AS id, " +  // 修改此处，将 goods_id 作为 id 返回
        "    lrod.`name`, " +
                "    CASE " +
                "        WHEN FIND_IN_SET(lrod.goods_id, REPLACE(REPLACE(lotr.return_ids, '[', ''), ']', '')) > 0 THEN '1' " +
                "        ELSE '0' " +
                "    END AS status " +
                "FROM " +
                "    litemall_rent_order_detail lrod " +
                "LEFT JOIN " +
                "    litemall_rent_order lro ON lro.id = lrod.rent_order_id " +
                "LEFT JOIN " +
                "    litemall_order_total_return_history lotr ON lotr.order_id = lro.id " +
                "WHERE " +
                "    lro.id = #{orderId} " +
                "    AND ( " +
                "        lotr.return_ids IS NULL " +
                "        OR FIND_IN_SET(lrod.goods_id, REPLACE(REPLACE(lotr.return_ids, '[', ''), ']', '')) > 0 " +
                "    );")
    List<ReturnGoodsList> getReturnGoodsListByOrderId(@Param("orderId") Long orderId);



    List<LitemallOrderTotalReturn>  selectWithHistories(RentPartQueryParam param);
 }
